package thread;

import java.util.*;

/**
 * 常用的集合:
 * ArrayList,LinkedList,HashSet都不是并发安全的集合
 */
public class SyncDemo5 {
    public static void main(String[] args) {
        /*
            Collections可以将现有的一个集合转换为并发安全的集合
         */
//        List<Integer> list = Collections.synchronizedList(new ArrayList<>());
        Set<Integer> set = Collections.synchronizedSet(new HashSet<>());
        Thread t1 = new Thread(){
            public void run(){
                for (int i = 0; i < 1000; i++) {
//                    list.add(i);
                    set.add(i);
                }
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                for (int i = 1000; i < 2000; i++) {
//                    list.add(i);
                    set.add(i);
                }
            }
        };
        t1.start();
        t2.start();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        System.out.println(list.size());
        System.out.println(set.size());
    }
}
